package com.gmall.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FanoutExchange_Queue_Config {

    public final static String FANOUT_EXCHANGE = "fanout_exchange";
    // 声明队列
    public final static String FANOUT_EX_QUEUE_1 = "fanout_queue_1";

    public final static String FANOUT_EX_QUEUE_2 = "fanout_queue_2";

    public final static String FANOUT_EX_QUEUE_3 = "fanout_queue_3";


    // 声明交换机
    @Bean("fanoutExchange")
    public Exchange fanoutExchange(){
        //Builder 设计模式
        return ExchangeBuilder.fanoutExchange(FANOUT_EXCHANGE)
                .durable(true)//默认RabbitMQ 重启以后， 交换机就删掉，除非是durable=true
                .build();
    }

    // 声明队列
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1(){
        //return new Queue(DIRECT_EX_QUEUE_SMS,true);
        return QueueBuilder.durable(FANOUT_EX_QUEUE_1).build();
    }
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2(){
        return QueueBuilder.durable(FANOUT_EX_QUEUE_2).build();
    }

    @Bean("fanoutQueue3")
    public Queue fanoutQueue3(){
        return QueueBuilder.durable(FANOUT_EX_QUEUE_3).build();
    }

    @Bean("fanoutBinding1")
    public Binding fanoutBinding1(@Qualifier("fanoutQueue1") Queue queue,
                                    @Qualifier("fanoutExchange") Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with("123").noargs();
    }

    @Bean("fanoutBinding2")
    public Binding fanoutBinding2(@Qualifier("fanoutQueue2") Queue queue,
                                  @Qualifier("fanoutExchange") Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with("456").noargs();
    }

    @Bean("fanoutBinding3")
    public Binding fanoutBinding3(@Qualifier("fanoutQueue3") Queue queue,
                                  @Qualifier("fanoutExchange") Exchange exchange){

        return BindingBuilder.bind(queue).to(exchange).with("anything").noargs();
    }

}
